﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;
using System;

namespace GameFramework.Controller
{
    public class TextDrawController
    {
        public TextDrawController(Game game, SpriteBatch spriteBatch, SpriteFont font, Color color,
            float verticalSpacing = 20f)
        {
            this.game = game;
            this.spriteBatch = spriteBatch;
            this.font = font;
            this.color = color;
            this.verticalSpacing = verticalSpacing;
            
        }

        void draw()
        {
            spriteBatch.Begin();

            for (int i = 0; i < lines.Count; ++i)
            {
                Line line = lines[i];
                float horizontalSpacing = 0;

                switch (line.Alignment)
                {
                    case TextAlignment.None:
                        break;
                    case TextAlignment.Left:
                        break;
                    case TextAlignment.Right:
                        horizontalSpacing = (float)game.Window.ClientBounds.Width - font.MeasureString(line.Text).X;
                        break;
                    case TextAlignment.Center:
                        horizontalSpacing = ((float)game.Window.ClientBounds.Width - font.MeasureString(line.Text).X)
                            / 2;
                        break;
                }

                spriteBatch.DrawString(font, line.Text, new Vector2(horizontalSpacing, i * verticalSpacing),
                    color);
            }

            spriteBatch.End();
        }

        public void WriteLine()
        {
            lines.Add(new Line("", TextAlignment.None));
        }

        public void WriteLine(string format, params object[] args)
        {
            lines.Add(new Line(string.Format(format, args), defaultAlignment));
        }

        public void WriteLine(TextAlignment alignment, string format, params object[] args)
        {
            lines.Add(new Line(string.Format(format, args), alignment));
        }

        public void Begin(TextAlignment defaultAlignment = TextAlignment.None)
        {
            this.defaultAlignment = defaultAlignment;
            
            lines.Clear();
        }

        public void Begin(Color color, TextAlignment defaultAlignment = TextAlignment.None)
        {
            this.defaultAlignment = defaultAlignment;
            this.color = color;

            lines.Clear();
            
        }

        public void End()
        {
            draw();
        }

        struct Line
        {
            public string Text;
            public TextAlignment Alignment;

            public Line(string text, TextAlignment alignment)
            {
                this.Text = text;
                this.Alignment = alignment;
            }
        }

        TextAlignment defaultAlignment;
        Color color;
        float verticalSpacing;
        SpriteFont font;
        SpriteBatch spriteBatch;
        List<Line> lines = new List<Line>();
        Game game;
    }

    public enum TextAlignment
    {
        None = 0,
        Left,
        Center,
        Right
    }
}
